<?php
class Api_Model_Ensayes extends App_Model
{
    private $_name = 'Fundicion_Ensayes_Reales_Mixturas';
    
	public function __construct() {
		parent::__construct();
	}

	public function getEnsaye($mixtura, $fetchMode=PDO::FETCH_OBJ) {
		$sql = <<<SQL
			SELECT 
                e.*, 
                m.Id as MIXTURA, 
                [PESO HUMEDO] 
            FROM 
                $this->_name e 
            INNER JOIN 
                Fundicion_Mixturas_Calculadas m 
            ON 
                m.id = e.[# Mixtura Tradicional] 
            WHERE e.[# Mixtura Tradicional] = ?
SQL;

		$stmt =  $this->_conn->prepare($sql);
        $stmt->execute(array(
            $mixtura
        ));

        // Regresar error si la consulta no pudo ejecutarse normalmente.
        if (FALSE === $stmt) {
            return $stmt;
        }

        return $stmt->fetch($fetchMode);
	}

	public function addEnsaye($ensaye) {
		
            // Denegar accion por edicion
            if (FALSE == EDITABLE) {
                return FALSE;
            }
            
            $sql = <<<SQL
                INSERT INTO $this->_name
                    (
                    [# Mixtura Tradicional],
                    [Fecha],
                    [MUESTRA], 
                    [% Fe3O4],
                    [g/t Ag REAL], 
                    [% Pb REAL],
                    [% Cu REAL],
                    [% SiO2 REAL], 
                    [% Fe REAL],
                    [TON FE REAL],
                    [% CaO Tot REAL], 
                    [TON CaO REAL],
                    [% CaO Utll REAL], 
                    [% Zn REAL], 
                    [% S REAL], 
                    [% S Leco], 
                    [% As REAL], 
                    [% Sb REAL],
                    [% Cd REAL], 
                    [% MgO REAL], 
                    [% Al203 REAL], 
                    [% Sn REAL], 
                    [CaO/SiO2 REAL], 
                    [Fe/SiO2 REAL], 
                    [As/Sb REAL], 
                    [Ag REAL],
                    [Cu/Pb],
                    [Cu/As REAL] )
                VALUES	(
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    ?
                )
SQL;

            
            $stmt = $this->_conn->prepare($sql);

            $stmt->execute(array(
                    $ensaye['# Mixtura Tradicional'], 
                    $ensaye['Fecha'], 
                    $ensaye['MUESTRA'], 
                    $ensaye['% Fe3O4'], 
                    $ensaye['g/t Ag REAL'], 
                    $ensaye['% Pb REAL'],
                    $ensaye['% Cu REAL'], 
                    $ensaye['% SiO2 REAL'], 
                    $ensaye['% Fe REAL'], 
                    $ensaye['TON FE REAL'],
                    $ensaye['% CaO Tot REAL'], 
                    $ensaye['TON CaO REAL'], 
                    $ensaye['% CaO Utll REAL'], 
                    $ensaye['% Zn REAL'], 
                    $ensaye['% S REAL'], 
                    $ensaye['% S Leco'], 
                    $ensaye['% As REAL'], 
                    $ensaye['% Sb REAL'],
                    $ensaye['% Cd REAL'], 
                    $ensaye['% MgO REAL'], 
                    $ensaye['% Al203 REAL'], 
                    $ensaye['% Sn REAL'], 
                    $ensaye['CaO/SiO2 REAL'], 
                    $ensaye['Fe/SiO2 REAL'], 
                    $ensaye['As/Sb REAL'], 
                    $ensaye['Ag REAL'], 
                    $ensaye['Cu/Pb'],
                    $ensaye['Cu/As REAL']
            ));

            return $stmt;
	}

	public function editEnsaye($mixtura, $ensaye) {
        // Denegar accion por edicion
        if (FALSE == EDITABLE) {
            return FALSE;
        }

		$sql = <<<SQL
			UPDATE $this->_name
				SET 
                    Fecha = ?,
                    MUESTRA = ?, 
                    [% Fe3O4] = ?,
                    [g/t Ag REAL] = ?,
                    [% Pb REAL] = ?,
                    [% Cu REAL] = ?,
                    [% SiO2 REAL] = ?,
                    [% Fe REAL] = ?,
                    [TON FE REAL] = ?,
                    [% CaO Tot REAL] = ?,
                    [TON CaO REAL] = ?,
                    [% CaO Utll REAL] = ?, 
                    [% Zn REAL] = ?,
                    [% S REAL] = ?,
                    [% S Leco] = ?,
                    [% As REAL] = ?,
                    [% Sb REAL] = ?,
                    [% Cd REAL] = ?,
                    [% MgO REAL] = ?,
                    [% Al203 REAL] = ?,
                    [% Sn REAL] = ?,
                    [CaO/SiO2 REAL] = ?,
                    [Fe/SiO2 REAL] = ?, 
                    [As/Sb REAL] = ?,
                    [Ag REAL] = ?, 
                    [Cu/Pb] = ?,
                    [Cu/As REAL] = ?
			 WHERE 
                [# Mixtura Tradicional] = ?
SQL;

		return $this->_conn->prepare($sql)
            ->execute(array(
                $ensaye['Fecha'],
                $ensaye['MUESTRA'],
                $ensaye['% Fe3O4'],
                $ensaye['g/t Ag REAL'],
                $ensaye['% Pb REAL'],
                $ensaye['% Cu REAL'],
                $ensaye['% SiO2 REAL'],
                $ensaye['% Fe REAL'],
                $ensaye['TON FE REAL'],
                $ensaye['% CaO Tot REAL'],
                $ensaye['TON CaO REAL'],
                $ensaye['% CaO Utll REAL'],
                $ensaye['% Zn REAL'],
                $ensaye['% S REAL'],
                $ensaye['% S Leco'],
                $ensaye['% As REAL'],
                $ensaye['% Sb REAL'],
                $ensaye['% Cd REAL'],
                $ensaye['% MgO REAL'],
                $ensaye['% Al203 REAL'],
                $ensaye['% Sn REAL'],
                $ensaye['CaO/SiO2 REAL'],
                $ensaye['Fe/SiO2 REAL'],
                $ensaye['As/Sb REAL'],
                $ensaye['Ag REAL'],
                $ensaye['Cu/Pb'],
                $ensaye['Cu/As REAL'],
                $mixtura
            ));
	}

	public function __destruct () {
		parent::__destruct();
	}
}
